package com.jsmile.cloud.tradecenter.order.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jsmile.cloud.msgcenter.api.feign.PayOrderServiceApi;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderCreate;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderFinish;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderRefund;
import com.jsmile.cloud.tradecenter.api.req.ReqPayOrder;
import com.jsmile.cloud.tradecenter.api.res.ResOrderList;
import com.jsmile.cloud.tradecenter.order.OrderApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.cache.kit.SpringKit;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "订单管理")
public class OrderServiceApiImpl extends AuthcController {

    private final OrderApplicationService orderApplicationService;

    @RequestMapping(value = "/feign/order/toPay", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->去支付", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult<ResOrderList> toPay(@RequestBody @Valid ReqPayOrder reqPayOrder, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqPayOrder.setCurUserId(jsmileContext.getUserId());
        reqPayOrder.setCurTenantId(jsmileContext.getTenantId());
        orderApplicationService.toPay(reqPayOrder);
        return JSmileResult.success(SpringKit.getBean(PayOrderServiceApi.class).toPay(reqPayOrder));
    }

    @RequestMapping(value = "/feign/order/create", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->创建订单", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult<ResOrderList> create(@RequestBody @Valid ReqOrderCreate reqOrderCreate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqOrderCreate.setCurUserId(jsmileContext.getUserId());
        reqOrderCreate.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(orderApplicationService.create(reqOrderCreate));
    }

    @RequestMapping(value = "/feign/order/preview", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->预览订单", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult<ResOrderList> preview(@RequestBody @Valid ReqOrderCreate reqOrderCreate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqOrderCreate.setCurUserId(jsmileContext.getUserId());
        reqOrderCreate.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(orderApplicationService.preview(reqOrderCreate));
    }

    @RequestMapping(value = "/feign/order/paySuccess", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->支付成功", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult paySuccess(@RequestBody @Valid ReqPayOrder reqOrderPay, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqOrderPay.setCurUserId(jsmileContext.getUserId());
        reqOrderPay.setCurTenantId(jsmileContext.getTenantId());
        orderApplicationService.paySuccess(reqOrderPay);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/order/finish", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->完成订单", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult paySuccess(@RequestBody @Valid ReqOrderFinish reqOrderFinish, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqOrderFinish.setCurUserId(jsmileContext.getUserId());
        reqOrderFinish.setCurTenantId(jsmileContext.getTenantId());
        orderApplicationService.finishOrder(reqOrderFinish);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/order/refund", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->订单退款", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult refund(@RequestBody @Valid ReqOrderRefund reqOrderRefund, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqOrderRefund.setCurUserId(jsmileContext.getUserId());
        reqOrderRefund.setCurTenantId(jsmileContext.getTenantId());
        orderApplicationService.refund(reqOrderRefund);
        return JSmileResult.success("退款成功");
    }
}
